package com.cskaoyan.demo6.service;

import com.cskaoyan.demo6.bean.model.MarketCategory;
import com.cskaoyan.demo6.bean.model.MarketGoods;
import com.cskaoyan.demo6.bean.model.MarketGoodsExample;
import com.cskaoyan.demo6.bean.vo.HomeIndexVo;
import com.cskaoyan.demo6.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description：
 * @Author：BLUEBLANK
 * @Date：2024/4/21 16:53
 */

@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public List<HomeIndexVo.HotGoodsListDTO> queryByHot(Integer offset, Integer hotLimit) {
        PageHelper.offsetPage(offset, hotLimit);

        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIsHotEqualTo(true);
        goodsExample.setOrderByClause("add_time desc");

        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(goodsExample);
        List<HomeIndexVo.HotGoodsListDTO> hotGoodsList = new ArrayList<>();
        marketGoods.forEach(goods -> {
            HomeIndexVo.HotGoodsListDTO hotGoodsListDTO = new HomeIndexVo.HotGoodsListDTO();
            BeanUtils.copyProperties(goods, hotGoodsListDTO);
            hotGoodsList.add(hotGoodsListDTO);
        });
        return hotGoodsList;
    }

    @Override
    public List<HomeIndexVo.NewGoodsListDTO> queryByNew(Integer offset, Integer newLimit) {
        PageHelper.offsetPage(offset, newLimit);

        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIsNewEqualTo(true);
        goodsExample.setOrderByClause("add_time desc");

        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(goodsExample);
        List<HomeIndexVo.NewGoodsListDTO> newGoodsList = new ArrayList<>();
        marketGoods.forEach(goods -> {
            HomeIndexVo.NewGoodsListDTO newGoodsListDTO = new HomeIndexVo.NewGoodsListDTO();
            BeanUtils.copyProperties(goods, newGoodsListDTO);
            newGoodsList.add(newGoodsListDTO);
        });
        return newGoodsList;
    }

    @Override
    public List<HomeIndexVo.FloorGoodsListDTO.GoodsListDTO> selectGoodsListByCategory(MarketCategory level, Integer catlogGoodsLimit) {
        List<MarketCategory> level2 = level.getChildren();
        List<Integer> categoryIdList = null;
        // 获取level2的id
        if(level2 != null && level2.size() > 0){
            categoryIdList = level2.stream().map(MarketCategory::getId).collect(Collectors.toList());
        }

        if(categoryIdList == null){
            return null;
        }

        PageHelper.offsetPage(0, catlogGoodsLimit);
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andCategoryIdIn(categoryIdList);
        List<MarketGoods> goodsList = marketGoodsMapper.selectByExample(goodsExample);

        List<HomeIndexVo.FloorGoodsListDTO.GoodsListDTO> goodsListDTOS = new ArrayList<>();
        goodsList.forEach(goods -> {
            HomeIndexVo.FloorGoodsListDTO.GoodsListDTO goodsListDTO = new HomeIndexVo.FloorGoodsListDTO.GoodsListDTO();
            BeanUtils.copyProperties(goods, goodsListDTO);
            goodsListDTOS.add(goodsListDTO);
        });
        return goodsListDTOS;
    }
}
